#include "ThreadManager.h"

CThreadManager::CThreadManager()
{
}

CThreadManager::~CThreadManager()
{

}

//函数名：
//函数功能:
//函数参数：
//返回值:
bool CThreadManager::Init()
{
    int result;

    Tool = CTool::GetInstance();
    if(!Tool)
        return false;

    SerialPort = CSerialPort::GetInstance();
    if(!SerialPort)
       return false;

    result = SerialPort->Init();
    if(!result)
        return false;

    InfoContainer = CInfoContainer::GetInstance();
    if(!InfoContainer)
        return false;

    result = InfoContainer->Init();
    if(!result)
        return false;

    Database = CDataBase::GetInstance();
    if(!Database)
        return false;

    result = Database->Init();
    if(!result)
        return false;


    RoomFlag = new int [(InfoContainer->GetRoom() + 1)];
    for(int CurRoom = 1; CurRoom < (InfoContainer->GetRoom() + 1) ; CurRoom++)
    {
        RoomFlag[CurRoom] = SMARTWORK;
    }

    //---------------------------------------//

    Tp2.WeiQueue = CWeiQueue::GetInstance();
    if(!Tp2.WeiQueue)
        return false;

    Queue = CQueue::GetInstance();
    if(!Queue)
        return false;

    //---------------------------------------//

    Tp1.DataCollect = new CDataCollect;
    if(!Tp1.DataCollect)
        return false;

    result = Tp1.DataCollect->Init();
    if(!result)
        return false;

//    Tp2.WeiBo = CWeiBo::GetWeiboInstance();
//    if(!Tp2.WeiBo)
//        return false;
//
//    result = Tp2.WeiBo->Init();
//    if(!result)
//        return false;

    CmdTransfer = new CCmdTransfer;
    if(!CmdTransfer)
        return false;

    result = CmdTransfer->Init();
    if(!result)
        return false;

    SmartMode = new CSmartMode;
    if(!SmartMode)
        return false;

    result = SmartMode->Init();
    if(!result)
        return false;

    TerminalControl = new CTerminalControl;
    if(!TerminalControl)
        return false;

    result = TerminalControl->Init();
    if(!result)
        return false;

    Tp1.CollectFlag = COLLECTFREE;
    Tp1.TerminalFlag = TERMINALFREE;
    if(pthread_mutex_init(&Tp1.ComLock, NULL) != 0)

    threadEnable1 = false;
    threadEnable2 = false;
    return true;
}

//函数名：
//函数功能:
//函数参数：
//返回值:
void CThreadManager::Cleanup()
{
    Tool->Cleanup();

    SerialPort->Cleanup();

    InfoContainer->Cleanup();

    Database->Cleanup();

    Tp2.WeiQueue->Cleanup();

    Queue->Cleanup();

    Tp1.DataCollect->Cleanup();
    if(Tp1.DataCollect)
    {
        delete Tp1.DataCollect;
        Tp1.DataCollect = 0;
    }

    SmartMode->Cleanup();
    if(SmartMode)
    {
        delete SmartMode;
        SmartMode = 0;
    }

    TerminalControl->Cleanup();
    if(TerminalControl)
    {
        delete TerminalControl;
        TerminalControl = 0;
    }

    CmdTransfer->Cleanup();
    if(CmdTransfer)
    {
        delete CmdTransfer;
        CmdTransfer = 0;
    }

    Tp2.WeiBo->Cleanup();
}



//函数名：
//函数功能:
//函数参数：
//返回值:
void * CThreadManager::DataCollectThread(void *arg)
{
    threadParameter1 * Tmp = (threadParameter1 *)arg;
    CDataCollect * DataCollect = Tmp->DataCollect;
    cout << "采集线程" << endl;
    while(1)
    {
        DataCollect->Run(Tmp->CollectFlag, Tmp->TerminalFlag, Tmp->ComLock);
        sleep(300); //休眠5分钟
    }
    return (void *)0 ;
}

//函数名：
//函数功能:
//函数参数：
//返回值:
void *DataCollectThreadFunc(void *arg)
{
    return DataCollectHandle->DataCollectThread(arg);
}

//函数名：
//函数功能:
//函数参数：
//返回值:
void * CThreadManager::ListenWeiBoThread(void *arg)
{
    cout << "监听线程" << endl;
    while(1)
    {
        //待加入代码
        cout << "WeiBo Listen Run..." << endl;
        sleep(10);
    }
    return (void*)0;
}

//函数名：
//函数功能:
//函数参数：
//返回值:
void *ListenWeiBoThreadFunc(void *arg)
{
    return ListenWeiBoHandle->ListenWeiBoThread(arg);
}

//函数名：
//函数功能:
//函数参数：
//返回值:
bool CThreadManager::MainControlerThread()
{
    int result;
    int & CollectFlag = Tp1.CollectFlag;
    int & TerminalFlag = Tp1.TerminalFlag;
    pthread_mutex_t & ComLock = Tp1.ComLock;
    cout << "主控线程" << endl;
    while(1)
    {
        usleep(100);//调试
        result = CmdTransfer->Run(RoomFlag);
        if(!result)
        {
            cout << "命令中转站模块运行失败" << endl;
            return false;
        }

//        result = SmartMode->Run();
//        if(!result)
//        {
//            cout << "智能模式控制模块运行失败" << endl;
//            return false;
//        }

        result = TerminalControl->Run(CollectFlag, TerminalFlag, ComLock);
        if(!result)
        {
            cout << "终端控制模块运行失败" << endl;
            return false;
        }
    }

    return true;
}

//函数名：
//函数功能:
//函数参数：
//返回值:
bool CThreadManager::Run()
{
    int result;

    result = pthread_create(&DataCollectThreadID, NULL, DataCollectThreadFunc, (void *)&Tp1);
    if(result < 0 )
    {
        cout << "数据采集线程启动失败" << endl;
        return false;
    }

    threadEnable1 = true;
    usleep(50000);

    result = pthread_create(&ListenWeiBoThreadID, NULL, ListenWeiBoThreadFunc, (void *)&Tp2);
    if(result < 0)
    {
        cout << "微博监听线程启动失败" << endl;
        return false;
    }

    threadEnable2 = true;
    usleep(50000);

    result = MainControlerThread();
    if(result < 0)
    {
        cout << "智能家居主线程出错" << endl;
        StopThread();
        return false;
    }

    return true;
}

//函数名：
//函数功能:
//函数参数：
//返回值:
bool CThreadManager::StopThread()
{
        int result;
        if ( !threadEnable1 )
        {
                cout << "数据采集线程已关闭" << endl;
                return true;
        }

        result = pthread_cancel(DataCollectThreadID);
        if ( result < 0 )
        {
                cout << "数据采集线程关闭失败" << endl;
                return false;
        }

        threadEnable1 = false;

        if ( !threadEnable2 )
        {
                cout << "微博监听线程已关闭" << endl;
                return true;
        }

        result = pthread_cancel(ListenWeiBoThreadID);
        if ( result < 0 )
        {
                cout << "微博监听线程关闭失败" << endl;
                return false;
        }

        threadEnable2 = false;

        return true;
}

